package com.scwatch.core.web.back.f7;

import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Model;
import com.scwatch.core.domain.Node;
import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.ModelService;
import com.scwatch.core.service.NodeQueryService;
import com.scwatch.core.support.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * NodeF7Controller
 * 
 * @author gjl
 * 
 */
@Controller
@RequestMapping("/core/node")
public class NodeF7Controller {
	/**
	 * 节点单选。信息选择主节点。
	 * 
	 * @param id
	 * @param excludeChildrenId
	 * @param isRealNode
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("choose_node_tree.do")
	public String f7NodeTree(Integer id, Integer excludeChildrenId,
			Boolean isRealNode, HttpServletRequest request,
			HttpServletResponse response, org.springframework.ui.Model modelMap) {
		Integer siteId = Context.getCurrentSiteId();
		List<Node> list = query.findList(siteId, null,null, isRealNode, null);
		Node bean = null, excludeChildrenBean = null;
		if (id != null) {
			bean = query.get(id);
		}
		if (excludeChildrenId != null) {
			excludeChildrenBean = query.get(excludeChildrenId);
		}

		modelMap.addAttribute("id", id);
		modelMap.addAttribute("excludeChildrenId", excludeChildrenId);
		modelMap.addAttribute("bean", bean);
		modelMap.addAttribute("excludeChildrenBean", excludeChildrenBean);
		modelMap.addAttribute("list", list);
		Servlets.setNoCacheHeader(response);
		return "core/node/choose_node_tree";
	}

	/**
	 * 节点单选。信息选择主节点。
	 * 
	 * @param id
	 * @param excludeChildrenId
	 * @param isRealNode
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("choose_node_tree_info_perms.do")
	public String f7NodeTreeInfoPerms(Integer id, Integer excludeChildrenId,
			Boolean isRealNode, HttpServletRequest request,String type,
			HttpServletResponse response, org.springframework.ui.Model modelMap) {
		Integer siteId = Context.getCurrentSiteId();
		List<Node> list = query.findList(siteId, null,null, isRealNode, null);
		Node bean = null, excludeChildrenBean = null;
		if (id != null) {
			bean = query.get(id);
		}
		if (excludeChildrenId != null) {
			excludeChildrenBean = query.get(excludeChildrenId);
		}

		User user = Context.getCurrentUser();
//		list = user.getInfoPermList(siteId, list);

        List<Node> newsnodelist=new ArrayList<>();
        Set set = new  HashSet();
        if(list!=null && list.size()>0){
            /*if("crawler".equals(type)){
                for(Node node:list){
                    //文档类型为新闻的 排除专题 市州 栏目
                    if(Constants.MODEL_FLAG_DOC_CRAWLER.equals(node.getInfoModel().getFlag())
                            && (node.getChildren()==null || node.getChildren().size()==0)){
                        for(Node n:node.getHierarchy()){
                            set.add(n);
                        }

                    }

                }
            }else{*/
                for(Node node:list){
                    //文档类型为新闻的 排除专题 市州 栏目
                    if((Constants.MODEL_FLAG_DOC_NEWS.equals(node.getInfoModel().getFlag())|| Constants.MODEL_FLAG_DOC_CRAWLER.equals(node.getInfoModel().getFlag())|| Constants.MODEL_FLAG_DOC_DRAFT.equals(node.getInfoModel().getFlag()))
                            && !Constants.SPECIAL_MANAGEMENT.equals(node.getNodeModel().getFlag())
                            && !Constants.SPECIAL_CATEGORY.equals(node.getNodeModel().getFlag())
                            && !Constants.SPECIAL_RECYCLE.equals(node.getNodeModel().getFlag())
                            && !Constants.MODEL_FLAG_CITY_DETAIL.equals(node.getNodeModel().getFlag())
                            && !Constants.MODEL_FLAG_CURRENTNEWS.equals(node.getNodeModel().getFlag())
                            && (node.getChildren()==null || node.getChildren().size()==0)){
                        for(Node n:node.getHierarchy()){
                            set.add(n);
                        }

                    }

                }
//            }
        }
        newsnodelist.addAll(set);
        newsnodelist=user.getNodePermList(siteId,newsnodelist);
		modelMap.addAttribute("id", id);
		modelMap.addAttribute("excludeChildrenId", excludeChildrenId);
		modelMap.addAttribute("bean", bean);
		modelMap.addAttribute("excludeChildrenBean", excludeChildrenBean);
		modelMap.addAttribute("list", newsnodelist);
		Servlets.setNoCacheHeader(response);
		return "core/node/choose_node_tree_info_perms";
	}

	/**
	 * 节点单选。信息选择主节点。
	 * 
	 * @param id
	 * @param excludeChildrenId
	 * @param isRealNode
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("choose_node_tree_node_perms.do")
	public String f7NodeTreeNodePerms(Integer id, Integer excludeChildrenId,
			Boolean isRealNode, HttpServletRequest request,
			HttpServletResponse response, org.springframework.ui.Model modelMap) {
		Integer siteId = Context.getCurrentSiteId();
		List<Node> list = query.findList(siteId, null,0, isRealNode, null);
		Node bean = null, excludeChildrenBean = null;
		if (id != null) {
			bean = query.get(id);
		}
		if (excludeChildrenId != null) {
			excludeChildrenBean = query.get(excludeChildrenId);
		}

		User user = Context.getCurrentUser();
		list = user.getNodePermList(siteId, list);

		modelMap.addAttribute("id", id);
		modelMap.addAttribute("excludeChildrenId", excludeChildrenId);
		modelMap.addAttribute("bean", bean);
		modelMap.addAttribute("excludeChildrenBean", excludeChildrenBean);
		modelMap.addAttribute("list", list);
		Servlets.setNoCacheHeader(response);
		return "core/node/choose_node_tree_node_perms";
	}

	/**
	 * 节点多选。信息选择副栏目。
	 * 
	 * @param ids
	 * @param isRealNode
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("choose_node_tree_multi.do")
	public String f7NodeTreeMulti(Integer[] ids, Boolean isRealNode,
			HttpServletRequest request, HttpServletResponse response,
			org.springframework.ui.Model modelMap) {
		Integer siteId = Context.getCurrentSiteId();
		List<Node> list = query.findList(siteId, null,null, isRealNode, null);
		List<Node> beans = new ArrayList<Node>();
		if (ids != null) {
			for (Integer id : ids) {
				beans.add(query.get(id));
			}
		}

		modelMap.addAttribute("ids", ids);
		modelMap.addAttribute("beans", beans);
		modelMap.addAttribute("list", list);
		Servlets.setNoCacheHeader(response);
		return "core/node/choose_node_tree_multi";
	}

	/**
	 * 节点多选。信息选择副栏目。
	 * 
	 * @param ids
	 * @param isRealNode
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("choose_node_tree_multi_info_perms.do")
	public String f7NodeTreeMultiInfoPerms(Integer[] ids, Boolean isRealNode,
			HttpServletRequest request, HttpServletResponse response,
			org.springframework.ui.Model modelMap) {
		Integer siteId = Context.getCurrentSiteId();
        Site site = Context.getCurrentSite();
		//查询主播的父级栏目
        Integer [] nodeIds=getNodeIdsByFlag(Constants.MODEL_FLAG_ANCHOR_DYNAMIC,site,null);
        Integer parentId=null;
        if(nodeIds!=null && nodeIds.length>0){
            parentId=nodeIds[0];
        }

		List<Node> list = query.findList(siteId, parentId,null, isRealNode, null);
		List<Node> beans = new ArrayList<Node>();
		if (ids != null) {
			for (Integer id : ids) {
				beans.add(query.get(id));
			}
		}
		User user = Context.getCurrentUser();
		list = user.getInfoPermList(siteId, list);
		List<Node> removeList=new ArrayList<Node>();
        for(Node n:list){
            if(Constants.MODEL_FLAG_ANCHOR_DYNAMIC.equals(n.getNodeModel().getFlag())){
                removeList.add(n);
            }
        }
        list.removeAll(removeList);
		modelMap.addAttribute("ids", ids);
		modelMap.addAttribute("beans", beans);
		modelMap.addAttribute("list", list);
		Servlets.setNoCacheHeader(response);
		return "core/node/choose_node_tree_multi_info_perms";
	}

	/**
	 * 节点多选。信息权限、节点权限
	 * 
	 * @param ids
	 * @param isRealNode
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("choose_node_tree_perms.do")
	public String f7NodeTreePerms(Integer[] ids, Boolean isRealNode,
			HttpServletRequest request, HttpServletResponse response,
			org.springframework.ui.Model modelMap) {
		Integer siteId = Context.getCurrentSiteId();
		List<Node> list = query.findList(siteId, null,null, isRealNode, null);
		List<Node> beans = new ArrayList<Node>();
		if (ids != null) {
			for (Integer id : ids) {
				beans.add(query.get(id));
			}
		}
		modelMap.addAttribute("ids", ids);
		modelMap.addAttribute("beans", beans);
		modelMap.addAttribute("list", list);
		Servlets.setNoCacheHeader(response);
		return "core/node/choose_node_tree_perms";
	}

    /**
     *  主播多选
     *
     * @param ids
     * @param isRealNode
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("choose_node_tree_multi_anchor_perms.do")
    public String f7NodeTreeMultiAnchor(Integer[] ids, Boolean isRealNode,
                                           HttpServletRequest request, HttpServletResponse response,
                                           org.springframework.ui.Model modelMap) {
        Integer siteId = Context.getCurrentSiteId();
        User user = Context.getCurrentUser();

        List<Node> beans = new ArrayList<Node>();
        if (ids != null) {
            for (Integer id : ids) {
                if(id != null) {
                    beans.add(query.get(id));
                }
            }
        }


        Map<String,String[]> paramMap=new HashMap<String,String[]>();
        paramMap.put("EQ_flag",new String[]{Constants.MODEL_FLAG_ANCHOR_LOOP_DYNAMICS});
        List<Model> models=modelService.findList(siteId,null,paramMap,null);

        paramMap=new HashMap<String,String[]>();
        String[] idsStr;
        if(models!=null && models.size()>0){
            idsStr=new String[models.size()];
            for(int i=0;i<models.size();i++){
                idsStr[i]=models.get(i).getId().toString();
            }
            paramMap.put("EQ_nodeModel.id",idsStr);
        }

        Sort paramSort=new Sort(Sort.Direction.ASC, "treeNumber", "id");
        List<Node>  anchorNodeList=query.findList(siteId,null,
                null,null,true,
                paramMap, paramSort);
        anchorNodeList= user.getInfoPermList(siteId, anchorNodeList);
        modelMap.addAttribute("ids", ids);
        modelMap.addAttribute("beans", beans);
        modelMap.addAttribute("list", anchorNodeList);
        Servlets.setNoCacheHeader(response);
        return "core/publish_center/choose_node_tree_multi_anchor_perms";
    }

    private Integer[] getNodeIdsByFlag(String modelFlag, Site site, User user) {
        Integer [] nodeIds=null;
        Map<String,String[]> paramMap=new HashMap<String,String[]>();
        paramMap.put("EQ_flag",new String[]{modelFlag});
        List<Model> models=modelService.findList(site.getId(),null,paramMap,null);

        paramMap=new HashMap<String,String[]>();
        String[] idsStr;
        if(models!=null && models.size()>0){
            idsStr=new String[models.size()];
            for(int i=0;i<models.size();i++){
                idsStr[i]=models.get(i).getId().toString();
            }
            paramMap.put("EQ_nodeModel.id",idsStr);
        }
        Sort paramSort=new Sort(Sort.Direction.ASC, "treeNumber", "id");
        List<Node>  currentNodeList=query.findList(site.getId(),null,
                null,null,true,
                paramMap, paramSort);
//        currentNodeList= user.getInfoPermList(site.getId(), currentNodeList);
        if(currentNodeList!=null){
            nodeIds=new Integer[currentNodeList.size()];
            for(int i=0;i<currentNodeList.size();i++){
                nodeIds[i]=(currentNodeList.get(i).getId());
            }
        }
        return  nodeIds;
    }

	@Autowired
	private NodeQueryService query;

    @Autowired
    private ModelService modelService;
}
